﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Guline.Web.CentralModels;
using Guline.Web.Central.Controllers;
using Guline.PaymentAPI;
using Newtonsoft.Json.Linq;

namespace Guline.Web.Central.Areas.User.Controllers
{
    public class CashController :  BaseController
    {
        //
        // GET: /User/Cash/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult CardCharge()
        {
            ViewBag.Type = new SelectList(_ListCardType, "ID", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult CardCharge(Card model)
        {
            if (ModelState.IsValid)
            {
                OnePay onepay = new OnePay();
                string result = onepay.sendPost("jr51ji4lxx3dkw65dps9", "clbpybcdaxc5h0wbba1v8zyaj86nf2t7", model.Type, model.PIN, model.Serial);
                JObject jojbect;
                try
                {
                    jojbect = JObject.Parse(result);
                    if (jojbect["status"].ToString() == "0")
                    {
                        ModelState.AddModelError("", "thanh cong~");
                        return RedirectToAction("CardChangreSuccess");
                    }
                    else
                    {
                        ModelState.AddModelError("", "loi~ " + jojbect["description"].ToString());
                    }
                }
                catch (Exception er)
                {
                    ModelState.AddModelError("", "loi~ " + er.Message);
                }
                
            }
            ViewBag.Type = new SelectList(_ListCardType, "ID", "Name",model.Type);
            return View();
        }
        public ActionResult CardChangreSuccess()
        {
            return View();
        }
    }
}
